﻿Public Class ctrChinhSuaTD

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        frmMain.TabControl1.TabPages.Remove(frmMain.TabControl1.SelectedTab)
    End Sub

    Private Sub ctrChinhSuaTD_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gvThucDon_Load()
        cmbLoaiMon_Load()
        cmbLoaiMon.Select()
    End Sub

    Private Sub gvThucDon_Load()
        Dim bus As ThucDonBUS = New ThucDonBUS()
        gvThucDon.DataSource = bus.GetTable()
    End Sub

    Private Sub cmbLoaiMon_Load()
        Dim bus As LoaiMonBUS = New LoaiMonBUS()
        cmbLoaiMon.DataSource = bus.GetTable()
        cmbLoaiMon.DisplayMember = "TenLoai"
        cmbLoaiMon.ValueMember = "MaLoai"
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim tmp As ThucDonDTO = New ThucDonDTO()
        tmp.MaLoai = cmbLoaiMon.SelectedValue.ToString()
        tmp.TenMon = txtTenMon.Text
        If cbThucUong.Checked = True Then tmp.ThucUong = True Else tmp.ThucUong = False
        tmp.DonGia = Integer.Parse(txtDonGia.Text)

        Dim bus As ThucDonBUS = New ThucDonBUS()
        bus.Insert(tmp)

        gvThucDon_Load()
        rdTatCa.Checked = True
    End Sub

    Private Sub btnLamMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamMoi.Click
        txtTenMon.Text = ""
        txtDonGia.Text = ""
        cbThucUong.Checked = False
        cmbLoaiMon.Select()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim tmp As ThucDonDTO = New ThucDonDTO()
        tmp.MaLoai = cmbLoaiMon.SelectedValue.ToString()
        tmp.TenMon = txtTenMon.Text
        If cbThucUong.Checked = True Then tmp.ThucUong = True Else tmp.ThucUong = False
        tmp.DonGia = Integer.Parse(txtDonGia.Text)
        tmp.MaMon = gvThucDon.CurrentRow.Cells(0).Value.ToString()

        Dim bus As ThucDonBUS = New ThucDonBUS()
        bus.Update(tmp)

        gvThucDon_Load()
        rdTatCa.Checked = True
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim tmp As ThucDonDTO = New ThucDonDTO()
        tmp.MaMon = gvThucDon.CurrentRow.Cells(0).Value.ToString()

        Dim bus As ThucDonBUS = New ThucDonBUS()
        bus.Delete(tmp)

        gvThucDon_Load()
        rdTatCa.Checked = True
    End Sub

    Private Sub gvThucDon_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvThucDon.SelectionChanged
        If gvThucDon.CurrentRow IsNot Nothing Then
            cmbLoaiMon.SelectedValue = gvThucDon.CurrentRow.Cells(1).Value.ToString()
            txtTenMon.Text = gvThucDon.CurrentRow.Cells(2).Value.ToString()
            If gvThucDon.CurrentRow.Cells(3).Value = True Then cbThucUong.Checked = True Else cbThucUong.Checked = False
            txtDonGia.Text = gvThucDon.CurrentRow.Cells(4).Value.ToString()
        End If
    End Sub

    Private Sub rdMon_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdMon.CheckedChanged
        Dim bus As GeneralBUS = New GeneralBUS()
        gvThucDon.DataSource = bus.GetTable("select * from THUCDON where ThucUong=False")
    End Sub

    Private Sub rdThucUong_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdThucUong.CheckedChanged
        Dim bus As GeneralBUS = New GeneralBUS()
        gvThucDon.DataSource = bus.GetTable("select * from THUCDON where ThucUong=True")
    End Sub

    Private Sub rdTatCa_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdTatCa.CheckedChanged
        gvThucDon_Load()
    End Sub
End Class
